package com.example.healthsystem.config;

import com.example.healthsystem.entity.user;
import com.example.healthsystem.exception.BadRequestException;
import com.example.healthsystem.model.vo.Result;
import com.example.healthsystem.util.JacksonUtils;
import com.example.healthsystem.util.JwtUtils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
//import org.springframework.security.authentication.*;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

//public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter {
//    private AuthenticationManager authenticationManager;
//
//    public JWTLoginFilter(String defaultFilterProcessesUrl,AuthenticationManager authenticationManager){
//        super(new AntPathRequestMatcher(defaultFilterProcessesUrl));
//        setAuthenticationManager(authenticationManager);
//    }
//
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request,
//                                                HttpServletResponse response)
//        throws AuthenticationException,IOException{
//
//        try{
//            if(!"POST".equals(request.getMethod())){
//                throw new BadRequestException("请求方法错误");
//            }
//            System.out.println("just test the input stream: "+request.getInputStream().toString());
//            user user= JacksonUtils.readValue(request.getInputStream(),user.class);
//
//            System.out.println("user is:"+user.toString());
//            return getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(),user.getPassword()));
//        }catch(BadRequestException exception){
//            response.setContentType("application/json;charset=utf-8");
//            Result result = Result.create(400, "非法请求");
//            PrintWriter out = response.getWriter();
//            out.write(JacksonUtils.writeValueAsString(result));
//            out.flush();
//            out.close();
//        }
//        return null;
//    }
//
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request,
//                                         HttpServletResponse response,
//                                         FilterChain chain,
//                                         Authentication auth) throws IOException, ServletException {
//
//        String jwt = JwtUtils.generateToken(auth.getName(), auth.getAuthorities());
//        response.setContentType("application/json;charset=utf-8");
//        user user = (user) auth.getPrincipal();
//        user.setPassword(null);
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("user", user);
//        map.put("token", jwt);
//
//        Result result = Result.ok("登录成功", map);
//        PrintWriter out = response.getWriter();
//        out.write(JacksonUtils.writeValueAsString(result));
//        out.flush();
//        out.close();
//
//    }
//
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request,
//                                              HttpServletResponse response,
//                                              AuthenticationException exception)
//        throws IOException{
//
//        response.setContentType("application/json;charset=utf-8");
//        String msg=exception.getMessage();
//
//        if (exception instanceof LockedException) {
//            msg = "账号被锁定";
//        } else if (exception instanceof CredentialsExpiredException) {
//            msg = "密码过期";
//        } else if (exception instanceof AccountExpiredException) {
//            msg = "账号过期";
//        } else if (exception instanceof DisabledException) {
//            msg = "账号被禁用";
//        } else if (exception instanceof BadCredentialsException) {
//            msg = "用户名或密码错误";
//        }
//
//        PrintWriter out=response.getWriter();
//        out.write(JacksonUtils.writeValueAsString(Result.create(401,msg)));
//        out.flush();
//        out.close();
//    }
//}

public class JWTLoginFilter{

}
